<template>
  <div>
    <el-dialog
      :title="dataForm.type == 'add'?'下发考卷':'编辑考卷'"
      :visible.sync="dialogVisible"
      width="25%"
      :show-close="false"
      :close-on-click-modal="false">
      <el-form ref="dataForm" :model="dataForm" :rules="dataForm_rules" label-width="110px">
          <el-form-item label="开始时间：" prop="ep_start_time">
            <el-date-picker
              v-model="dataForm.ep_start_time"
              type="datetime"
              placeholder="选择日期"
              value-format="yyyy-MM-dd HH:mm:ss">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="结束时间：" prop="ep_end_time">
            <el-date-picker
              v-model="dataForm.ep_end_time"
              type="datetime"
              placeholder="选择日期"
              value-format="yyyy-MM-dd HH:mm:ss">
            </el-date-picker>
          </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
      <el-button size="mini" @click="closeDialog">取 消</el-button>
      <el-button size="mini" type="primary" @click="submitInfo">确 定</el-button>
    </span>
    </el-dialog>
  </div>
</template>
<script>
  import{updateRoomPaper,addRoomPaper} from '@/api/invigilate'
  export default{
    components: {},
    props:["isShowRoomPaperDialog","roomPaperInfo"],
      created(){
         if(this.roomPaperInfo.type == "add"){
             this.addRoomPaperInfo();
         }else if(this.roomPaperInfo.type == "edit"){
             this.editRoomPaperInfo();
         }
      },
      data(){
          return {
            dialogVisible:this.isShowRoomPaperDialog,

            dataForm:{
                tr_id:0,
                ep_id:0,
                ep_start_time:"",
                ep_end_time:""
            },

            dataForm_rules:{
              ep_start_time:[{ required: true, message: '不能为空', trigger: 'change' }],
              ep_end_time:[{ required: true, message: '不能为空', trigger: 'change' }]
            }
          }
      },

      methods:{
        addRoomPaperInfo(){
            this.dataForm.tr_id = this.roomPaperInfo.tr_id;
            this.dataForm.ep_id = this.roomPaperInfo.ep_id;
            this.dataForm.type = this.roomPaperInfo.type;
        },

        editRoomPaperInfo(){
            this.dataForm = JSON.parse(JSON.stringify(this.roomPaperInfo));
        },

        closeDialog(){
          this.dialogVisible = false;
          this.$emit("closeDialog","closeRoomPaper");
        },

        submitInfo(){
          this.$refs["dataForm"].validate((valid) => {
              if(valid){
                if(this.dataForm.ep_end_time <= this.dataForm.ep_start_time){
                  this.$message({
                    message: '结束时间不能小于开始时间',
                    type: 'warning'
                  });
                  return;
                }
                if(this.dataForm.type == "add"){
                  addRoomPaper(this.dataForm).then(res=>{
                    if(res.code == 200){
                      this.$message({
                        message: '下发成功',
                        type: 'success'
                      });
                      this.dialogVisible = false;
                      this.$emit("closeDialog","addRoomPaper");
                    }
                  })
                }else if(this.dataForm.type == "edit"){
                  updateRoomPaper(this.dataForm).then(res=>{
                      if(res.code == 200){
                        this.$message({
                          message: '修改成功',
                          type: 'success'
                        });
                        this.closeDialog();
                      }
                  })
                }
              }
          });
        }

      }
  }
</script>
